#define TEXT_SIZE 4*1024  /* = PAGE_SIZE, size of each message */

#define TEXT_NUM 4      /* maximal number of mesages */

    /* total size can not exceed current shmmax,

       or an 'invalid argument' error occurs when shmget */



/* a demo structure, modified as needed */

struct people{

    char name[20];

    char id[20];

    double grade;

};



struct shared_struct {

    

    int written; /* flag = 0: buffer writable; others: readable */

    

    // for Queue

    int in;

    int out;

    struct people queue[TEXT_NUM]; // the size of queue is TEXTNUM 3

    

};





void Push(struct shared_struct *shared,struct people new_peo){

    if((shared->in + 1) % TEXT_NUM == shared->out){

        // queue is full

        printf("The queue is full!\nFail to push!!\n");

        return;

    }

    else{

        shared->queue[shared->in] = new_peo;

        shared->in = (shared->in + 1)%TEXT_NUM;

        printf("Success to Push!\n");

        return;

    }

}



void Pop(struct shared_struct *shared){

    if(shared->in == shared->out){

        //empty

        printf("The queue is empty!\nFail to pop!!\n");

        return;

    }

    else{

        struct people this_peo = shared->queue[shared->out];

        printf("This people info:\nName:%s Id:%s Grade:%lf\n",this_peo.name,this_peo.id,this_peo.grade);

        shared->out = (shared->out + 1) % TEXT_NUM;

        printf("Success to Pop!\n");

        return;

    }

}



void PrintQueue(struct shared_struct *shared){

    printf("\nThe queue info is:\n");

    printf("The queue size is: %d\n",shared->in - shared->out);

    int p = shared->out;

    if(p == shared->in){

        printf("The queue is empty!\n");

        return;

    }

    while (shared->in != p) {

        struct people this_peo = shared->queue[p];

        p = (p + 1) % TEXT_NUM;

        printf("This people info:\nName:%s\tId:%s\tGrade:%lf\n",this_peo.name,this_peo.id,this_peo.grade);

    }

}



#define PERM S_IRUSR|S_IWUSR|IPC_CREAT



#define ERR_EXIT(m) \

    do { \

        perror(m); \

        exit(EXIT_FAILURE); \

    } while(0)





